﻿using AutoMapper;
using B.S.BaseData.Domain.Entities;
using B.S.BaseData.ErrorCode;
using B.S.BaseData.Infrastructure.Interfaces;
using B.S.BaseData.Read.API.Command.RoleCommand;
using B.S.BaseData.Read.API.DTO;
using MediatR;

namespace B.S.BaseData.Read.API.CommandHandler.RoleHandler
{
    public class QueryEmpHandler : IRequestHandler<QueryEmpCommand, APIResult<List<EmpDTO>>>
    {
        private readonly IBaseRepository<User> userRep;
        private readonly IMapper mapper;

        public QueryEmpHandler(IBaseRepository<User> userRep,IMapper mapper)
        {
            this.userRep = userRep;
            this.mapper = mapper;
        }
        public Task<APIResult<List<EmpDTO>>> Handle(QueryEmpCommand request, CancellationToken cancellationToken)
        {
           APIResult<List<EmpDTO>> result = new APIResult<List<EmpDTO>>();
            var emp = userRep.GetAll().Where(x=>x.DepartmentId==request.DepartmentId);
            result.Code = APIEnum.Success;
            result.Msg = "查询成功";
            result.Data=mapper.Map<List<EmpDTO>>(emp);

            return Task.FromResult(result);
        }
    }
}
